package com.nutshare.lt;

import com.nutshare.lt.util.ValueComparator;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Stat
{

    public LTWorkspace ltw;

    private static Pattern p = Pattern.compile("^\\d+$");
    private static Pattern pdate = Pattern.compile("^(\\d+)\\s+([\\d\\.]+)$");
    public ArrayList<Result> results = new ArrayList<Result>();

    public ArrayList<TreeMap<Integer, Integer>> popularResults = new ArrayList<TreeMap<Integer, Integer>>();



    public Stat(LTWorkspace ltw)
    {
        this.ltw = ltw;
    }

    public void readFromFile(String file)
    {
        FileReader fs = null;
        try
        {
            fs = new FileReader(file);

        } catch (FileNotFoundException e)
        {
            System.out.println(e.getMessage());
            System.exit(1);
        }

        BufferedReader in = new BufferedReader(fs);

        String line;

        try
        {
            String num = null, date=null, res = null;

            while ((line = in.readLine()) != null)
            {
                line = line.trim();

                if(line.equals("") || line.equals(" "))
                {
                    continue;
                }

                Matcher m = p.matcher(line);
                if(m.find())
                {

                    res = line;

                    if(num!=null && date!=null && res!=null)
                    {
                        String splitted = splitDigits(res);

                        Result ltRes = new Result(ltw.getCapacity(), ltw.getCount(), num, date);
                        ltRes.parseString(splitted);
                        results.add(ltRes);
                    }

                    num = null;
                    date = null;
                    res = null;

                    continue;
                }

                Matcher mdate = pdate.matcher(line);

                if(!mdate.find())
                {
                    continue;
                }

                num = mdate.group(1);
                date = mdate.group(2);

            }
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
            System.exit(1);
        }


    }

    public void calculateTotalPopular()
    {

        HashMap<Integer, Integer> totalPopular = new HashMap<Integer, Integer>();


        for(int i=(results.size()-1); i>=0; i--)
        {
            Result next = results.get(i);
            Iterator<Integer> items = next.iterator();

            while(items.hasNext())
            {
                Integer digit = items.next();
                int stat = 0;
                if(totalPopular.containsKey(digit))
                {
                    stat = totalPopular.get(digit);
                }
                stat++;
                totalPopular.put(digit, new Integer(stat));
            }

            HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
            for(int l =0; l <ltw.getCapacity(); l++)
            {
                Integer value;
                Integer key = new Integer(l+1);
                if(totalPopular.containsKey(key))
                {
                    value = totalPopular.get(key);
                }
                else
                {
                    value = new Integer(0);
                }
                map.put(key, value);
            }

            ValueComparator vc = new ValueComparator(map);
            TreeMap<Integer, Integer> treeMap = new TreeMap<Integer, Integer>(vc);
            treeMap.putAll(map);

//            System.out.println(next);
//            System.out.println(map);
//            System.out.println(treeMap);
//            System.out.println(" ");
            popularResults.add(treeMap);
        }

    }




    private String splitDigits(String in)
    {
        String ret = "";

        int added =0;
        for (int i = 0; i < in.length(); i++)
        {

            if ((in.charAt(i) == '0' && added == 0) || added == 2)
            {
                if (i > 0)
                {
                    ret += " ";
                }
                added = 0;
            }

            ret += in.charAt(i);
            added++;
        }

        return ret;
    }

}
